package lib

import (
	"fmt"
	"log"
	"regexp"
	"strings"
)

type (
	GovPub    struct{}
	CzSz      struct{}
	SsZc      struct{}
	ZfJj      struct{}
	Ppp       struct{}
	Zcjd      struct{}
	SjdXxzl   struct{} // 十九大专题学习资料
	XjpJhXxzl struct{} // 习总书记讲话学习资料
)

func (gp *GovPub) Rule() map[string]fieldContainer {
	result := make(map[string]fieldContainer)
	result["title"] = fieldContainer{
		Selector: ".viewList .til a",
	}
	result["date"] = fieldContainer{
		Selector: ".viewList span.time",
	}
	result["link"] = fieldContainer{
		Selector: ".viewList li a",
		Attr:     "href",
	}

	return result
}

func (*CzSz) Rule() map[string]fieldContainer {
	result := make(map[string]fieldContainer)
	result["title"] = fieldContainer{
		Selector: ".lists_ty li a",
	}
	result["date"] = fieldContainer{
		Selector: ".lists_ty li span",
	}
	result["link"] = fieldContainer{
		Selector: ".lists_ty li a",
		Attr:     "href",
		Pipeline: func(args interface{}) interface{} {
			var s []string
			str, ok := args.(string)
			if !ok {
				log.Fatal("类型不合法")
				return ""
			}
			return strings.Join(append(s, "http://www.gdczt.gov.cn/zwgk/sgjf/sjqk/", str), "")
		},
	}

	return result
}

func (*SsZc) Rule() map[string]fieldContainer {
	result := make(map[string]fieldContainer)
	result["title"] = fieldContainer{
		Selector: ".lists_ty li a",
	}
	result["date"] = fieldContainer{
		Selector: ".lists_ty li span",
	}
	result["link"] = fieldContainer{
		Selector: ".lists_ty li a",
		Attr:     "href",
		Pipeline: func(args interface{}) interface{} {
			var s []string
			str, ok := args.(string)
			if !ok {
				log.Fatal("类型不合法")
				return ""
			}
			return strings.Join(append(s, "http://www.gdczt.gov.cn/zwgk/sgjf/jsjf/sszc/", str), "")
		},
	}

	return result
}

func (*ZfJj) Rule() map[string]fieldContainer {
	result := make(map[string]fieldContainer)
	result["title"] = fieldContainer{
		Selector: ".lists_ty li a",
	}
	result["date"] = fieldContainer{
		Selector: ".lists_ty li span",
	}
	result["link"] = fieldContainer{
		Selector: ".lists_ty li a",
		Attr:     "href",
		Pipeline: func(args interface{}) interface{} {
			var s []string
			str, ok := args.(string)
			if !ok {
				log.Fatal("类型不合法")
				return ""
			}
			return strings.Join(append(s, "http://www.gdczt.gov.cn/zwgk/sgjf/jsjf/jjml/", str), "")
		},
	}

	return result
}

func (*Ppp) Rule() map[string]fieldContainer {
	result := make(map[string]fieldContainer)
	result["title"] = fieldContainer{
		Selector: ".lists_ty li a",
	}
	result["date"] = fieldContainer{
		Selector: ".lists_ty li span",
	}
	result["link"] = fieldContainer{
		Selector: ".lists_ty li a",
		Attr:     "href",
		Pipeline: func(args interface{}) interface{} {
			var s []string
			str, ok := args.(string)
			if !ok {
				log.Fatal("类型不合法")
				return ""
			}
			return strings.Join(append(s, "http://www.gdczt.gov.cn/zwgk/sgjf/PPP/", str), "")
		},
	}

	return result
}

func (*Zcjd) Rule() map[string]fieldContainer {
	result := make(map[string]fieldContainer)

	result["title"] = fieldContainer{
		Selector: ".lists_ty li a",
	}
	result["date"] = fieldContainer{
		Selector: ".lists_ty span",
	}
	result["link"] = fieldContainer{
		Selector: ".lists_ty li a",
		Attr:     "href",
		Pipeline: func(args interface{}) interface{} {
			var s []string
			str, ok := args.(string)
			if !ok {
				log.Println("类型不合法")
				return ""
			}
			return strings.Join(append(s, "http://www.gdczt.gov.cn/zwgk/zcjd/", str), "")
		},
	}

	return result
}

var re = regexp.MustCompile(`http://[\w]+\.12371\.cn/(\d+)/(\d+)/(\d+)/[^shtml]+shtml`)

func (sjdXxzl *SjdXxzl) Rule() map[string]fieldContainer {
	result := make(map[string]fieldContainer)
	result["title"] = fieldContainer{
		Selector: ".showMoreNChildren li p a",
	}

	result["link"] = fieldContainer{
		Selector: ".showMoreNChildren li p a",
		Attr:     "href",
	}

	return result
}

func (XjpJhXxzl *XjpJhXxzl) Rule() map[string]fieldContainer {
	result := make(map[string]fieldContainer)
	result["title"] = fieldContainer{
		Selector: ".showMoreNChildren li p a",
	}

	result["date"] = fieldContainer{
		Selector: ".showMoreNChildren li p a",
		Attr:     "href",
		Pipeline: func(args interface{}) interface{} {
			str, ok := args.(string)
			if !ok {
				log.Println("类型不合法")
				return ""
			}

			matches := re.FindStringSubmatch(str)
			if len(matches) < 4 {
				return ""
			}

			return fmt.Sprintf("%s-%s-%s", matches[1], matches[2], matches[3])
		},
	}

	result["link"] = fieldContainer{
		Selector: ".showMoreNChildren li p a",
		Attr:     "href",
		Pipeline: func(args interface{}) interface{} {
			str, ok := args.(string)
			if !ok {
				log.Println("类型不合法")
				return ""
			}

			if !strings.Contains(str, "2018") {
				return ""
			}

			return str
		},
	}

	return result
}
